package com.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.magicindicator.buildins.UIUtil;
import com.module.commonui.R;
import com.ns.yc.yccardviewlib.shadow.ShadowLayout;
import com.tange.base.toolkit.DimenUtil;
import com.tg.appcommon.android.TGResources;


public class TGButton extends FrameLayout {

    private Button button;
    private ShadowLayout shadowLayout;
    private ImageView icon;

    private boolean shadowVisible = true;
    private String text;
    private Drawable drawable;


    public TGButton(Context context) {
        super(context);
        initContentView(context);
    }

    public TGButton(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TGButton(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initParam(context, attrs);
        initContentView(context);
    }

    @Override
    public void setOnClickListener(@Nullable OnClickListener l) {
        button.setOnClickListener(l);
    }

    public void setButtonEnable(boolean enabled){
        button.setEnabled(enabled);
        shadowLayout.setAlpha(enabled? 1.f : 0.2f);
    }

    public void setText(int resId) {
        button.setText(resId);
    }

    private void initParam(Context context, @Nullable AttributeSet attrs) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.TGButton);
        text = typedArray.getString(R.styleable.TGButton_inside_button_text);
        drawable = typedArray.getDrawable(R.styleable.TGButton_inside_button_icon);
        shadowVisible = typedArray.getBoolean(R.styleable.TGButton_inside_button_shadow_visible, true);
        if (drawable == null) {
            drawable = context.getResources().getDrawable(R.drawable.ic_tange_global_icon_next_step_arrow);
        }

        typedArray.recycle();
    }

    private void initContentView(Context context) {
        View view = LayoutInflater.from(context).inflate(R.layout.layout_tange_global_button_with_text_or_icon, this, true);

        icon = view.findViewById(R.id.button_inside_icon);

        shadowLayout = view.findViewById(R.id.sl_rectangle_shadow_layout);

        button = view.findViewById(R.id.button_with_text);

        button.setText(text);

        int layoutStyle = context.getResources().getInteger(R.integer.layout_tange_global_login_page_style);
        int style = context.getResources().getInteger(R.integer.layout_tange_global_login_page_button_style);

        if (TGResources.isRedirectEnabled() && TGResources.readUiCustomizeSwitch(context)) {
            String s = TGResources.readRedirectConfig(context, "LoginActivity");
            if ("standard".equals(s)) {
                layoutStyle = 0;
            } else if ("background".equals(s)) {
                layoutStyle = 1;
            }

            style = TGResources.readLoginButtonStyle(context);
        }


        if (layoutStyle == 0) {
            if (style == 0) {
                button.setText(text);
                button.setCompoundDrawables(null, null, null, null);
                icon.setVisibility(View.GONE);
            } else if (style == 1) {
                button.setText("");
                icon.setVisibility(View.VISIBLE);
                icon.setImageDrawable(drawable);
            } else {
                button.setText("样式参数有误");
                icon.setVisibility(View.GONE);
            }
        } else if (layoutStyle == 1) {
            ViewGroup.LayoutParams lp = button.getLayoutParams();
            lp.width = UIUtil.getScreenWidth(context) - 2 * DimenUtil.dp2px(context, 30);
            button.setLayoutParams(lp);
            shadowLayout.disabledShadow();

            if (style == 0) {
                button.setText(text);
                button.setCompoundDrawables(null, null, null, null);
                icon.setVisibility(View.GONE);
            } else if (style == 1) {
                button.setText("");
                icon.setVisibility(View.VISIBLE);
                icon.setImageDrawable(drawable);
            } else {
                button.setText("样式参数有误");
                icon.setVisibility(View.GONE);
            }
        }


    }
}
